//
//  CheckPhoneCodeController.m
//  FinancialProduct
//  新用户注册2/3   老用户手机验证码登录
//  Created by Stella on 2017/8/25.
//  Copyright © 2017年 maq. All rights reserved.
//

#import "HandleNetLogin.h"
#import "CKPuslichTool.h"
#import "ValidateTools.h"
#import "SVProgressHUD.h"
#import "PassSetController.h"
#import "DBAcquireAuthCodeButton.h"
#import "CheckPhoneCodeController.h"

@interface CheckPhoneCodeController (){
     NSString * sendPhoneNo;
}
@property (weak, nonatomic) IBOutlet UILabel *titleLa;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet DBAcquireAuthCodeButton *codeButton;
@property (weak, nonatomic) IBOutlet UILabel *reminder1;
@property (weak, nonatomic) IBOutlet UILabel *reminder2;
@property (weak, nonatomic) IBOutlet UILabel *reminder;
@property (weak, nonatomic) IBOutlet UIButton *NextBt;
@property (strong,nonatomic)NSDate *oldDate;
@property (strong,nonatomic)NSString *mCode;

@end

@implementation CheckPhoneCodeController
- (IBAction)backTop:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)MoveTextfileContent:(UITextField *)textfile{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 27)];
    
    imageView.contentMode = UIViewContentModeCenter;
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    textfile.layer.cornerRadius = 5;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    sendPhoneNo=@"";
    self.titleLa.text=@"新用户注册（2/3）";
    self.reminder1.text=@"";
    self.reminder2.text=@"";
    self.reminder.text=@"";
    self.phoneTF.layer.cornerRadius=5;
    self.codeTF.layer.cornerRadius=5;
    self.codeButton.layer.cornerRadius=5;
    self.NextBt.layer.cornerRadius=5;
    [self setTextfile:self.phoneTF img:[UIImage imageNamed:@"iPhone-3(G)"]];
    [self MoveTextfileContent:self.codeTF];
    [self.phoneTF  addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.codeTF  addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
- (IBAction)goNext:(id)sender {
    
    self.reminder1.text=@"";
    self.reminder2.text=@"";
    
    if (self.codeTF.text.length == 0) {
         self.reminder2.text=@"*请输入验证码";
        return;
    }
    if (![self.codeTF.text isEqualToString:_mCode]) {
        self.reminder2.text=@"*验证码验证失败";
        return;
    }
    
    NSDate *currentDate=[NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    
    NSInteger interval = [zone secondsFromGMTForDate:currentDate];
    
    NSDate *localDate1 = [currentDate dateByAddingTimeInterval:interval];
    NSDate *localDate2 = [_oldDate dateByAddingTimeInterval:interval];
    
    //计算时间差间隔
    NSTimeInterval timeBetween = [localDate1 timeIntervalSinceDate:localDate2];
    NSLog(@"timeBetween---%f",timeBetween);
    if (timeBetween>60*10) {
        self.reminder2.text=@"*验证码已失效";
        return;
    }
    //获取验证码手机与当前手机号是否一致
    NSString *CodePhone=self.phoneTF.text;
    
    if (![CodePhone isEqualToString:sendPhoneNo]) {
        self.reminder2.text=@"*获取验证码";
        return;
    }else{
        //验证码验证成功==》去设置密码
        PassSetController *vc=(PassSetController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"PassSetController"];
        vc.isFrome=@"register";
        NSMutableDictionary *infoDic=[NSMutableDictionary dictionaryWithDictionary:self.userinfo];
        [infoDic setObject:self.phoneTF.text forKey:@"phone"];
        vc.userinfo=infoDic;
        [self.navigationController pushViewController:vc animated:YES];
    }

    
    
    
}
-(IBAction)tarStartButtonTimers:(id)sender{
    
    
    self.reminder1.text=@"";
    self.reminder2.text=@"";
    if ( self.phoneTF.text.length == 0) {
        self.reminder1.text=@"*请输入手机号";
        return;
    }
    
    if ( ![ValidateTools validateMobile:self.phoneTF.text]) {
        self.reminder1.text=@"*手机号码格式不正确";
        return;
    }
    
    
    [HandleNetLogin RegisterUsePhone:self.phoneTF.text andName:self.userinfo[@"name"] Success:^(NSDictionary *data) {
        //验证注册成功---》设置密码
        _mCode = data[@"VerificationCode"];;
        sendPhoneNo=self.phoneTF.text;
        _oldDate=[NSDate date];
        [_codeButton tarStartButtonTimer];
        
       
    } Failure:^(NSString *errorInfo) {
        self.reminder1.text=[NSString stringWithFormat:@"*%@",errorInfo];
    }];
    
    
    
}
-(void)setTextfile:(UITextField *)textfile img:(UIImage *)img{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 27)];
    imageView.image =img;
    imageView.contentMode = UIViewContentModeCenter;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(imageView.frame.size.width-5, 6, 1, textfile.frame.size.height-6*2)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [textfile addSubview:view];
    
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    //    textfile.layer.borderColor = [UIColor whiteColor].CGColor;
    textfile.layer.cornerRadius = 5;
    
    
    
}

-(void)textFieldDidChange:(UITextField *)textField{
    if (textField==_phoneTF) {
        if (textField.text.length>11) {
            textField.text = [textField.text substringToIndex:11];
        }
    }else{
        if (textField.text.length>20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
